const apply_model = require('../db/apply_model')
const token_verify = require('../utils/token_verify')
const jwt_config = require('../config/jwt_config')
const qiniu = require('qiniu')


const add_apply = async (ctx) => {
    const apply = {apply_title,user_name,apply_content,apply_xs,time_ks,time_jz,classroom,apply_xz,apply_zz,img_url,room_bh,room_name,room_time,user_id} = ctx.request.body
    try {
      const result = await apply_model.apply_insert(apply)
      // console.log(result)
      // ctx.body = apply_model.apply_insert
      ctx.body = result
    } catch (error) {
      ctx.throw(400, error)
    }
  };
  // 生成七牛网token
  const add_token = async (ctx) => {
    // 创建上传凭证
    const accessKey = 'Y4lNlmzw63lZcUbiqBXaZ67fK2Tb9O8zHmKDWGND'
    const secretKey = 'p9OQdROM9ocUdECYUBJjgLz-PJYUWN7gl_5BLItl'
    const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
    const options = {
      scope: 'the-second-classroom',
      expires: 7200
    }
    const putPolicy = new qiniu.rs.PutPolicy(options);
    const uploadToken = putPolicy.uploadToken(mac);
    
    ctx.body = uploadToken
      };

// 获取全部活动信息
const get_AllAppl = async (ctx) => {
  try {

    const result = await apply_model.get_apply_all()
    ctx.body = result

  } catch (error) {
    ctx.throw(400, error)
  }
}
// 查询文章详情
// const get_apply = async(ctx) => {
//   const {id} = ctx.params
//   try {
//     const apply = await apply_model.apply_select_by_id(id)
//     ctx.body = apply
//   } catch (error) {
//     ctx.throw(400, error.code)
//   }
// }
// 获取活动详情信息
const get_apply = async (ctx, next) => {
  const {id} = ctx.params
  try {
    const apply = await apply_model.apply_select_by_id(id)
    ctx.body = apply
  } catch (error) {
    ctx.throw(400, error)
  }
}

// 用户参与活动
const post_apply_inst = async (ctx) => {
  const apply_xq = {apply_id,user_id,apply_title,time_ks,time_inst} = ctx.request.body
  try {
    const result = await apply_model.apply_insert_inst(apply_xq)
    // console.log(result)
    // ctx.body = apply_model.apply_insert
    ctx.body = result
  } catch (error) {
    ctx.throw(400, error)
  }
}


const get_apply_top6 = async (ctx) => {
  try {
    const applys = await apply_model.get_apply_top6_select()
    // console.log('========')
    ctx.body = applys
  } catch (error) {
    ctx.throw(400, error)
  }
}

const post_apply_select = async (ctx) => {
  const apply_input = {apply_title,apply_xz} = ctx.request.body
  // console.log(apply_input)
  try {
    const result = await apply_model.apply_insert_select(apply_input)
    ctx.body = result
  } catch (error) {
    ctx.throw(400, error)
  }
}

// 查看用户是否已参与当前活动
const Duplicate = async(ctx) => {
  const DuplicateData = {user_id, apply_id} = ctx.request.body
  try{
    const data = await apply_model.Duplicate_select(DuplicateData)
    ctx.body = data
  }catch{
    ctx.throw(400,'未参与')
  }
  
}
// 请求已参与项目
const post_apply_insted = async (ctx, next) => {
  const {user_id} = ctx.request.body
  try {
    const applyList = await apply_model.applyinsted_select_by_id(user_id)
    ctx.body = applyList
  } catch (error) {
    ctx.throw(400, error)
  }
}

const delete_apply_insted =  async (ctx, next) => {
  const {apply_id} = ctx.request.body
  try {
    const applyList = await apply_model.delect_apply_insert(apply_id)
    ctx.body = applyList
  } catch (error) {
    ctx.throw(400, error)
  }
}

module.exports = {
    // 
    add_apply,
    add_token,
    get_AllAppl,
    get_apply,
    get_apply_top6,
    post_apply_inst,
    post_apply_select,
    Duplicate,
    post_apply_insted,
    delete_apply_insted
  }